//
//  RequestNetwork.m
//  DWBeautiful
//
//  Created by mac on 2018/9/10.
//  Copyright © 2018年 杨洋. All rights reserved.
//

#import "RequestNetwork.h"

@implementation RequestNetwork

+ (void)GET:(NSString *)URLString parameters:(id)parama succeed:(void (^)(id responseObject))succeed failure:(void (^)(NSError *error))failure
{
    //创建网络请求管理对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //申明返回的结果是json类型
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",@"text/plain", nil];
    //发送网络请求(请求方式为GET)
//    URLString = [NSString stringWithFormat:@"%@/%@",baseUrl,URLString];
    NSLog(@"请求地址：%@",URLString);
    NSLog(@"请求参数：%@",parama);
    [manager GET:URLString parameters:parama progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        succeed(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [MyTools showMessage:@"与服务器失去连接"];
        failure(error);
        NSLog(@"error = %@",error);
    }];
}

+ (void)POST:(NSString *)URLString parameters:(id)parama succeed:(void (^)(id responseObject))succeed failure:(void (^)(NSError *error))failure
{
    //创建网络请求管理对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    //申明返回的结果是json类型
//    manager.responseSerializer = [AFJSONResponseSerializer serializer];
//    //申明请求的数据是json类型
//    /*
//
//     AFHTTPRequestSerializer            //二进制的数据格式 （默认的）
//     AFJSONRequestSerializer            //JSON
//     AFPropertyListRequestSerializer   //PList(是一种特殊的XML,解析起来相对容易)
//
//     */
//    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//    //如果报接受类型不一致请替换一致text/html或别的
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
////    //发送网络请求(请求方式为POST)
    URLString = [NSString stringWithFormat:@"%@%@",MXMainUrl,URLString];
    NSLog(@"请求地址：%@",URLString);
    NSLog(@"请求参数：%@",parama);
    [manager POST:URLString parameters:parama progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        succeed(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [MyTools showMessage:@"与服务器失去连接"];
        failure(error);
        NSLog(@"error = %@",error);
    }];
}

@end
